//
//  SEChatTextCell.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/9.
//

import UIKit
import YYText

let kChatTextLeft: CGFloat = 72                                         // 消息在左边的时候， 文字距离屏幕左边的距离
let kChatTextMaxWidth: CGFloat = UIScreen.screenWidth - kChatTextLeft - 82 // 消息在右边， 82：文本离屏幕左的距离，  82：文本离屏幕右的距离
let kChatTextMarginTop: CGFloat = 13                                    // 文字的顶部和气泡顶部相差 13 像素
let kChatTextMarginBottom: CGFloat = 13                                 // 文字的底部和气泡底部相差 13 像素
let kChatTextMarginLeft: CGFloat = 16                                   // 文字的左边 和气泡的左边相差 16 ,包括剪头部门
let kChatBubbleWidthBuffer: CGFloat = kChatTextMarginLeft * 2           // 气泡比文字的宽度多出的值
let kChatBubbleBottomTransparentHeight: CGFloat = 7                     // 气泡底部的透明高度 7
let kChatBubbleHeightBuffer: CGFloat = kChatTextMarginTop + kChatTextMarginBottom  // 文字的顶部 + 文字底部距离
let kChatBubbleImageViewHeight: CGFloat = 48                            // 气泡最小高 48 ，防止拉伸图片变形
let kChatBubbleImageViewWidth: CGFloat = 50                             // 气泡最小宽 50 ，防止拉伸图片变形
let kChatBubblePaddingTop: CGFloat = 0
let kChatBubbleMarginLeft: CGFloat = 5                                  // 气泡和头像的 gap 值：5
let kChatMargin: CGFloat = 8                                        // 左边的间隙
let kChatBubbleLeft: CGFloat = kChatAvatarMarginLeft + kChatAvatarWidth + kChatBubbleMarginLeft  // 气泡距离屏幕左的距

class SEChatTextCell: SEChatBaseCell {
    
    override func update(with viewModel: SEChatViewModel) {
        super.update(with: viewModel)
        
        contentLabel.linePositionModifier = viewModel.richTextLinePositionModifier
        
        if let richTextLayout = viewModel.richTextLayout {
            contentLabel.textLayout = richTextLayout
        }
        
        if let richTextAttributedString = viewModel.richTextAttributedString {
            contentLabel.attributedText = richTextAttributedString
        }
        
        bubbleImageView.image = viewModel.bubbleImage
        
        setNeedsLayout()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let vm = viewModel else { return }
        
        self.contentLabel.size = vm.richTextLayout?.textBoundingSize ?? .zero
        
        if vm.fromMe {
            bubbleImageView.left = UIScreen.screenWidth - kChatAvatarMarginLeft - kChatAvatarWidth - kChatBubbleMarginLeft - max(contentLabel.width + kChatBubbleWidthBuffer, kChatBubbleImageViewWidth)
            activityIndicator.right = bubbleImageView.left - kChatMargin
            resendButton.right = bubbleImageView.left - kChatMargin
        } else {
            bubbleImageView.left = kChatBubbleLeft
            activityIndicator.left = bubbleImageView.right + kChatMargin
            resendButton.left = bubbleImageView.right + kChatMargin
        }
        
        bubbleImageView.width = max(contentLabel.width + kChatBubbleWidthBuffer, kChatBubbleImageViewWidth)
        bubbleImageView.height = max(contentLabel.height + kChatBubbleHeightBuffer + kChatBubbleBottomTransparentHeight, kChatBubbleImageViewHeight)
        bubbleImageView.top = kChatBubblePaddingTop
        
        contentLabel.top = bubbleImageView.top + kChatTextMarginTop
        contentLabel.left = bubbleImageView.left + kChatTextMarginLeft
        
        activityIndicator.centerY = bubbleImageView.centerY
        resendButton.centerY = bubbleImageView.centerY
    }
    
    /// 解析点击文字
    /// - Parameters:
    ///   - label:     YYLabel
    ///   - textRange: 高亮文字的 NSRange，不是 Range
    private func didTapRichLabelText(_ label: YYLabel, textRange: NSRange) {
        // 解析 userInfo 的文字
        guard let attributedString = label.textLayout?.text,
              textRange.location >= attributedString.length,
              let hightlight = attributedString.yy_attribute(YYTextHighlightAttributeName, at: UInt(textRange.location)) as? YYTextHighlight,
              let info = hightlight.userInfo, info.count > 0 else { return }
        
        if let phone = info[kChatTextKeyPhone] as? String {
//            delegate?.cellDidTappedPhone(self, phoneString: phone)
        }
        
        if let urlString = info[kChatTextKeyURL] as? String {
//            delegate?.cellDidTappedLink(self, linkString: urlString)
        }
    }
    
    @IBOutlet weak var contentLabel: SEChatTextLabel!  {
        didSet {
            contentLabel.numberOfLines = 0
            contentLabel.backgroundColor = .clear
            contentLabel.textVerticalAlignment = .top
            contentLabel.ignoreCommonProperties = true
            contentLabel.highlightTapAction = { [weak self] (containerView, text, range, rect) in
                guard let self = self else { return }
                self.didTapRichLabelText(self.contentLabel, textRange: range)
            }
        }
    }
    @IBOutlet weak var bubbleImageView: UIImageView!
}

class SEChatTextLabel: YYLabel {}
